package com.love.marriage.userTopConfigInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.common.enums.ResultEnum;
import com.love.marriage.common.enums.TransactionType;
import com.love.marriage.common.exceptions.SystemException;
import com.love.marriage.userBalanceInfo.service.UserBalanceInfoService;
import com.love.marriage.userBalanceInfo.vo.UserBalanceInfoAddVo;
import com.love.marriage.userBalanceInfo.vo.UserBalanceInfoVo;
import com.love.marriage.userTopConfigInfo.entity.UserTopConfigInfo;
import com.love.marriage.userTopConfigInfo.mapper.UserTopConfigInfoMapper;
import com.love.marriage.userTopConfigInfo.service.UserTopConfigInfoService;
import com.love.marriage.userTopConfigInfo.vo.UserTopConfigInfoPageVo;
import com.love.marriage.userTopConfigInfo.vo.UserTopConfigInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户置顶配置表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-24 20:49:00
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserTopConfigInfoServiceImp extends ServiceImpl<UserTopConfigInfoMapper, UserTopConfigInfo> implements UserTopConfigInfoService {

    private final UserTopConfigInfoMapper userTopConfigInfoMapper;

    private final UserBalanceInfoService userBalanceInfoService;

    @Override
    public Page<UserTopConfigInfoPageVo> getPage(Long pageNum, Long pageSize, UserTopConfigInfoPageVo userTopConfigInfoVo) {
        Page<UserTopConfigInfoPageVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userTopConfigInfoMapper.getPage(page, userTopConfigInfoVo);
    }

    @Override
    public UserTopConfigInfoVo queryUserTopConfigInfo(Long id) {
        return userTopConfigInfoMapper.queryUserTopConfigInfo(id);
    }

    @Override
    public UserTopConfigInfoVo queryUserTopConfigInfoByUserId(Long userId) {
        return userTopConfigInfoMapper.queryUserTopConfigInfoByUserId(userId);
    }

    @Override
    public UserTopConfigInfoVo addUserTopConfigInfo(UserTopConfigInfoVo userTopConfigInfoVo) {
        UserTopConfigInfo userTopConfigInfo = new UserTopConfigInfo();
        BeanUtils.copyProperties(userTopConfigInfoVo, userTopConfigInfo);
        userTopConfigInfoMapper.insert(userTopConfigInfo);
        UserTopConfigInfoVo res = new UserTopConfigInfoVo();
        BeanUtils.copyProperties(userTopConfigInfo, res);
        return res;
    }

    @Override
    public UserTopConfigInfoVo updateUserTopConfigInfo(UserTopConfigInfoVo userTopConfigInfoVo) {
        UserTopConfigInfo userTopConfigInfo = new UserTopConfigInfo();
        BeanUtils.copyProperties(userTopConfigInfoVo, userTopConfigInfo);
        userTopConfigInfoMapper.updateById(userTopConfigInfo);
        return userTopConfigInfoVo;
    }

    @Override
    public Boolean deleteUserTopConfigInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userTopConfigInfoMapper.deleteBatchIds(idArr);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserTopConfigInfoVo top(UserTopConfigInfoVo userTopConfigInfoVo) {
        if (userTopConfigInfoVo.getDurationDays() == 0) {
            throw new SystemException(ResultEnum.PARAM_ERROR);
        }
        // 判断余额是否充足
        UserBalanceInfoVo userBalanceInfoVo = userBalanceInfoService.queryUserBalanceInfoByUserId(userTopConfigInfoVo.getUserId());
        if (userBalanceInfoVo == null || userBalanceInfoVo.getBalance().compareTo(userTopConfigInfoVo.getPaymentAmount()) < 0) {
            throw new SystemException(ResultEnum.USER_NO_ENOUGH_BALANCE);
        }
        // 查询当前人是否有置顶配置
        UserTopConfigInfoVo dbTop = queryUserTopConfigInfoByUserId(userTopConfigInfoVo.getUserId());
        if (dbTop == null) {
            // 没有则新增
            userTopConfigInfoVo.setStartTime(LocalDate.now().atTime(0, 0, 0));
            userTopConfigInfoVo.setEndTime(userTopConfigInfoVo.getStartTime().plusDays(userTopConfigInfoVo.getDurationDays() - 1));
            userTopConfigInfoVo.setDurationDays(userTopConfigInfoVo.getDurationDays());
            dbTop = addUserTopConfigInfo(userTopConfigInfoVo);
        } else {
            // 有则判断是否过期
            if (dbTop.getEndTime().isBefore(LocalDateTime.now())) {
                // 如果过期则直接更新开始结束时间
                dbTop.setStartTime(LocalDate.now().atTime(0, 0, 0));
                dbTop.setEndTime(dbTop.getStartTime().plusDays(userTopConfigInfoVo.getDurationDays() - 1));
                dbTop.setDurationDays(userTopConfigInfoVo.getDurationDays());
                updateUserTopConfigInfo(dbTop);
            } else {
                // 如果没过期，则添加结束时间
                dbTop.setEndTime(dbTop.getEndTime().plusDays(userTopConfigInfoVo.getDurationDays()));
                dbTop.setDurationDays(dbTop.getDurationDays() + userTopConfigInfoVo.getDurationDays());
                updateUserTopConfigInfo(dbTop);
            }
        }
        // 扣减余额
        UserBalanceInfoAddVo userBalanceInfoAddVo = new UserBalanceInfoAddVo();
        userBalanceInfoAddVo.setUserId(userTopConfigInfoVo.getUserId());
        userBalanceInfoAddVo.setAmount(userTopConfigInfoVo.getPaymentAmount());
        userBalanceInfoService.reduceAmount(userBalanceInfoAddVo, dbTop.getId(), TransactionType.TOP.getCode(), TransactionType.TOP.getValue());
        return dbTop;
    }
}
